//
//  HWFocusJiemianViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/9/14.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWFocusJiemianViewController.h"
#import "HWFocusGuanzhuCollectionViewCell.h"
#import "CLModel.h"


@interface HWFocusJiemianViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSString *p; //分页
@property(strong,nonatomic) NSString *max_p; // 最大分页
@property(strong,nonatomic) NSMutableArray *arrayDS;


@end

@implementation HWFocusJiemianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self addControlsview];
    [self addrefreshControl];
    
    
}


- (void)setupUI  {
    
    
    self.view.backgroundColor = RGB(0, 0, 0);
    
    
}


- (void)shuxingshuju {
    
    [self.maincollectionView.mj_header beginRefreshing];
    
}


- (void)addrefreshControl {
    
    self.maincollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    [self.maincollectionView.mj_header beginRefreshing];
    self.maincollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    // 任务中心
    
}


- (void)underRefresh {
    DLog(@"精选");
    
    
    
    self.p  = @"1";
    
    self.arrayDS = [[NSMutableArray alloc]init];
    NSString *uid = [HWLocallyStoredData getLoginUserID];
    NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:signStr];
    
    
    NSString *type = @"1";
    
    NSDictionary *parametes = @{
        
        
        @"ct":@"app",
        @"ac":@"video",
        @"uid":uid,
        @"t" :tString,
        @"sign" :signMD5,
        @"type":type,
        @"p": self.p,
        @"ios" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
        @"p" :   self.p,
        
        
        
    };
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        if ([codeStr intValue] == 1) {
            // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
            
            self.arrayDS  = [CLModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            
            [self.maincollectionView reloadData];
            
            
            
            
            
        } else {
            
            [self.maincollectionView reloadData];
            // [MBProgressHUD showError:@"收藏失败"];
            [MBProgressHUD showError:response[@"message"]];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        
        
    }];
    
    
    
    
    
    
    
}



- (void)onRefresh  {
    
    
    if ([self.p intValue] > [self.max_p intValue] ) {
        
        [MBProgressHUD showError:@"请稍后在加载数据"];
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        return ;
    }
    // self.arrayDS = [[NSMutableArray alloc]init];
    NSString *uid = [HWLocallyStoredData getLoginUserID];
    NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:signStr];
    
    
    NSString *type = @"1";
    
    NSDictionary *parametes = @{
        
        
        @"ct":@"app",
        @"ac":@"video",
        @"uid":uid,
        @"t" :tString,
        @"sign" :signMD5,
        @"type":type,
        @"p":self.p,
        @"ios" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
        
        
        
    };
    
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        
        //  [self.tableView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        
        if ([codeStr intValue] == 1) {
            // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
            
            NSMutableArray  *muArr   = [CLModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            //  self.tableView.contentOffset = CGPointMake(0, self.arrayDS.count*self.view.frame.size.height);
            
            
            //  int a = self.arrayDS.count;
            
            for (int i =0 ; i < muArr.count; i++) {
                [self.arrayDS addObject:muArr[i]];
                
            }
            
            
            
            [self.maincollectionView reloadData];
            
            
            //               self.tableView.contentSize =  CGSizeMake(kScreenW, self.tableView.frame.size.height*self.arrayDS.count);
            //               //   self.tableView.contentOffset =
            //
            //               NSIndexPath *index = [NSIndexPath indexPathForRow:a inSection:0];
            //
            //
            //             [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];
            //
            
            
            
            
            
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            
            
        } else {
            
            
            // [MBProgressHUD showError:@"收藏失败"];
            [MBProgressHUD showError:response[@"message"]];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
        
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        [MBProgressHUD showError:@"收藏失败"];
        
    }];
    
    
    
    
    
    
    
    
    
    
}












#pragma  mark -----  添加控件
- (void)addControlsview {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 15*CFLScaleValue;
    
    CGFloat juanju  = (kScreenW - 24*CFLScaleValue*2-62*CFLScaleValue*3-78*CFLScaleValue)/2;
    layout.minimumInteritemSpacing = juanju-1;
    
    layout.minimumInteritemSpacing = 15*CFLScaleValue;
    //    layout.headerReferenceSize = CGSizeMake(kScreenW, 142);  //设置headerView大小
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 5+30+5, kScreenW, kScreenH-TabBarHeight-CF_StatusBarHeight-5-30-5) collectionViewLayout:layout];
    // self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
    self.maincollectionView.alwaysBounceVertical = YES;
    self.maincollectionView.backgroundColor = [UIColor whiteColor];
    // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
    self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 20*CFLScaleValue, 0, 20*CFLScaleValue);
    //   [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
    //  [self.maincollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWFocusGuanzhuCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    self.maincollectionView.backgroundColor = RGB(0, 0, 0);
    self.maincollectionView.userInteractionEnabled = YES;
    [self.view  addSubview:self.maincollectionView];
    
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    
    return  self.arrayDS.count;
    //    if (self.dataMuArr.count > 0 ) {
    //         return self.dataMuArr.count-2;
    //    }
    
    
    
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWFocusGuanzhuCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    //   cell.backgroundColor =[UIColor yellowColor];
    
    
    CLModel *model =  self.arrayDS[indexPath.row];
    
    cell.model  = model;
    
    [cell.quexiaoguanzhuBut addTarget:self action:@selector(quexiaoguanzhu:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    return cell;
    
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((kScreenW-40*CFLScaleValue-15*CFLScaleValue)/2,245);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  {
    
    CLModel *model =  self.arrayDS[indexPath.row];
    
    
    HWGameDetailController *game = [[HWGameDetailController alloc]init];
    game.gameId = model.gid;
    [[[HWViewUtils  getCurrentVC] navigationController]  pushViewController:game animated:YES];
    
    
    
}








- (void)quexiaoguanzhu:(UIButton *)sender {
    
    
    NSLog(@"%@",[[[sender superview] superview]superview]);
    
    
    HWFocusGuanzhuCollectionViewCell *cell = (HWFocusGuanzhuCollectionViewCell *)[[[sender superview] superview]superview];
    
    NSIndexPath *indexPath =   [self.maincollectionView indexPathForCell:cell];
    
    CLModel *model =  self.arrayDS[indexPath.row];
    
    
    NSString *type =  @"1";
    
    
    
    
    
    NSString *uid = [HWLocallyStoredData getLoginUserID];
    NSString *tString = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    //  NSString *gidStr = [NSString stringWithFormat:@"%@",self.gameId];
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tString,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:signStr];
    
    
    
    NSDictionary *parametes = @{
        
        @"uid":uid,
        @"t":tString,
        @"sign":signMD5,
        @"id":model.id,
        @"ct":@"app",
        @"ac":@"video_guanzhu",
        @"type":type,
        @"ios" : @"1",
        @"member_id":[HWLocallyStoredData getCode],
        
        
        
        
    };
    
    
    [MBProgressHUD show:@"正在取消关注"];
    
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
        
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        
        [MBProgressHUD hideHUD];
        
        if ([codeStr intValue] == 1) {
            // [MBProgressHUD showMessage:@"收藏成功" toView:self.view];
            
            
            //    self.model.zan
            
            if ([type isEqualToString:@"1"]) {
                
                
                
                [self.maincollectionView.mj_header beginRefreshing];
                
            } else {
                
                
                
            }
            
            
            
            
            
        } else {
            
            
            // [MBProgressHUD showError:@"收藏失败"];
            [MBProgressHUD showError:response[@"message"]];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
        [MBProgressHUD showError:@"收藏失败"];
        
    }];
    
    
    
    
    
    
    
    
    
    
    
    
    
}





@end
